package com.example.shop.controller.backstage;

import com.example.shop.model.Role;
import com.example.shop.model.User;
import com.example.shop.service.RoleService;
import com.example.shop.utils.PageBean;
import com.example.shop.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/backstage")
public class UserRoleController {
    @Resource
    private RoleService roleService;

    /**
     * 修改用户权限
     */
    @PostMapping("/updateUserRole")
    public Map<String, String> updateUserRole(@RequestBody User user) {
        Map<String, String> map = new HashMap<>();
        int count = roleService.updateUserRole(user);
        if (count > 0) {
            map.put("message", "成功");
        } else {
            map.put("message", "失败");
        }
        return map;
    }

    /**
     * 获取权限列表
     */
    @GetMapping("/getRole")
    public Result<PageBean<Role>> getRole(int pageNo, int pageSize, String search) {
        Result<PageBean<Role>> res = new Result<>();
        PageBean<Role> pageBean = roleService.getRolePageBean(pageNo, pageSize, search);
        res.setCode("0");
        res.setData(pageBean);
        return res;
    }
}